package com.tarena.lbs.marketing.web.consumers;


import com.tarena.lbs.marketing.web.service.UserCouponsService;
import com.tarena.lbs.pojo.basic.event.LocationStoreEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class StoresLocationConsumer {
    @Autowired
    private UserCouponsService userCouponsService;
    @StreamListener("stores-location-input")
    public void autoReleaseCoupons(LocationStoreEvent event){
        //TODO 利用消息对象event 当做 key值 设置一个redis锁 setnx event对象 value EX 5
        //TODO 锁的key值 最好的方式使用的就是消息Id
        log.info("自动发放优惠券消费逻辑开始执行");
        //手动管理业务抛出的异常,由于消费逻辑 不需要保证100%成功
        try{
            userCouponsService.autoReleaseCoupons(event);
        }catch (Exception e){
            log.error("本次消费失败,用户id:{},店铺ids:{}",event.getUserId(),event.getStoreIds(),e);
        }
    }
}
